package com.lmqb.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmqb.bean.LmAccount;
import com.lmqb.bean.LmUser;
import com.lmqb.bean.LmUserBank;
import com.lmqb.bean.LmUserInfo;
import com.lmqb.bean.LmWithdraw;
import com.lmqb.dao.LmAccountLogMapper;
import com.lmqb.dao.LmAccountMapper;
import com.lmqb.dao.LmCompanyAccountMapper;
import com.lmqb.dao.LmUserBankMapper;
import com.lmqb.dao.LmUserInfoMapper;
import com.lmqb.dao.LmUserMapper;
import com.lmqb.dao.LmWithdrawMapper;
import com.lmqb.entry.Withdraw;
import com.lmqb.page.PageTool;
import com.lmqb.page.WithdrawPager;
import com.lmqb.service.CheckWithDrawService;
import com.lmqb.util.StrUtil;


@Service("checkWithDrawService")
public class CheckWithDrawImp implements CheckWithDrawService {

	@Autowired
	private LmWithdrawMapper widthdrawDao;
	
	@Autowired
	private LmUserInfoMapper userInfoDao;
	
	@Autowired
	private LmUserBankMapper userBankDao;
	
	@Autowired
	private LmAccountMapper accountDao;
	
	@Autowired
	private LmUserMapper lmUserDao;
	
	@Autowired
    private	LmCompanyAccountMapper  lmCompanyAccountDao;
	

  /**
   * @show 提现审核
   * 
   * @param WithdrawPager
   * 
   * @return WithdrawPager
   * 
   * */
  public WithdrawPager selectCheckWithdrawPager(WithdrawPager pagerModel) {
	  
	  List<LmWithdraw> list=widthdrawDao.selectAll(pagerModel);
		List<LmUser> users=lmUserDao.selectAllUser();
		List<LmUserInfo> userInfos=userInfoDao.selectAll();
		List<LmUserBank> banks=userBankDao.selectAllUser();
		List<LmAccount> accounts=accountDao.selectAllUser();
		List<Withdraw> withdraws=new ArrayList<Withdraw>();
		for(int i=0;i<list.size();i++){
			
			Withdraw withdraw=new Withdraw();
			for(int j=0;j<accounts.size();j++){
				if(accounts.get(j).getUserId().equals(list.get(i).getUserId())){
					withdraw.setBalance(accounts.get(j).getAccountBalance());
					withdraw.setSumMoney(accounts.get(j).getTotalWithdraw());
					withdraw.setFreezenMoney(accounts.get(j).getFreezenMoney());
				}
			}
			for(int k=0;k<banks.size();k++){
				if(banks.get(k).getBankId().equals(list.get(i).getBankId())){
					withdraw.setBankNum(banks.get(k).getBankAccount());
				}
			}
			for(int m=0;m<users.size();m++){
				if(list.get(i).getUserId().equals(users.get(m).getUserId())){
					withdraw.setMobile(users.get(m).getLoginMobile());
					
				withdraw.setUserName(users.get(m).getNickName());
				
					break;
				}
			}
			for(int n=0;n<userInfos.size();n++){
				if(userInfos.get(n).getUserId().equals(list.get(i).getUserId())){
					withdraw.setRealName(userInfos.get(n).getUserName());//用户名
				}
			}
			withdraw.setWithdrawNo(list.get(i).getWithdrawNo());
			withdraw.setMoney(list.get(i).getWithdrawMoney());
			withdraw.setStatus(list.get(i).getPayStatus());
			withdraw.setCheckStatus(list.get(i).getCheckStstus());
			withdraw.setUserId(list.get(i).getUserId());
			withdraw.setWithdrawTime(StrUtil.intToTime(list.get(i).getCreateTime()));
			withdraw.setCreatTime(list.get(i).getCreateTime());
			System.out.println(withdraw.getRealName());
			withdraws.add(withdraw);
		}
		pagerModel.setList(withdraws);
		int count = widthdrawDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
		}

  /**
   * @show 审核通过
   * 
   * @param WithdrawPager
   * 
   * @return WithdrawPager
   * 
   * */
  public int checkSrtatusUpdatePass(String withdrawNo,BigDecimal withdrawMoney,BigDecimal withdrawFee,
          Integer userId,String userName,String bankNum,Integer creatTime){
	  withdrawFee=new BigDecimal("1.00");
	  BigDecimal  tradAmount=withdrawMoney.subtract(withdrawFee);
	  int checkSrtatusUpdatePass = widthdrawDao.checkSrtatusUpdatePass(withdrawNo);
	  lmCompanyAccountDao.insertCheckSrtatusUpdatePass(withdrawNo, tradAmount, userId, userName, bankNum, creatTime);
	  
	 return checkSrtatusUpdatePass;
	  
	   }
  
   /**
    * @show 审核不通过
    * 
    * @param WithdrawPager
    * 
    * @return WithdrawPager
    * 
    * */ 
  public int checkSrtatusUpdateNoPass(BigDecimal withdrawMoney,String withdrawNo,BigDecimal freeznMoney,
          BigDecimal balance,Integer userId){
	  
	  int checkSrtatusUpdateNoPass = widthdrawDao.checkSrtatusUpdateNoPass(withdrawNo);
	  //不通过的两个数据逻辑处理
	  freeznMoney=freeznMoney.subtract(withdrawMoney);
	  balance=balance.add(withdrawMoney);
	  accountDao.checkSrtatusUpdateNoPass(freeznMoney,balance , userId);
		return checkSrtatusUpdateNoPass;
		  
		   }
}
